<?php

namespace app\api\controller;

use app\common\controller\Api;
use wechat\WeiXin;
use think\Config;
use wechat\WechatCallbackapiTest;
use function GuzzleHttp\json_encode;
use function GuzzleHttp\json_decode;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = '*';
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $weixin = new WeiXin;
        $wechat = Config::get('wechat');
        $result = $weixin->getEwm($wechat,'https://whgame.ipow.cn/wuhan/');
        $this->success('请求成功',$result,'200','json');
    }

    
    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        define("TOKEN","ipowtest");
        $wechatobj = new WechatCallbackapiTest;
        $wechatobj->valid();
       
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
        $txt = "Bill Gates\n";
        fwrite($myfile, $txt);
        fclose($myfile);
        $nonce = $_GET['nonce'];
        $token = 'ipowtest';
        $timestamp = $_GET['timestamp'];
        $echostr   = $_GET['echostr'];
        $signature = $_GET['signature'];
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        $str = sha1( implode( $array ) );
        if( $str == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }

    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $obj = new \SplDoublyLinkedList;
        //把新的节点数据添加到链表的顶部（Top）
        $obj->push(1);
        $obj->push(2);
        $obj->push(3);
        //把新的节点数据添加到链表的底部（Bottom）
        $obj->unshift(10);
        //rewind操作用于把节点指针指向Bottom所在节点
        $obj->rewind();
        //获取节点指针指向的节点（当前节点）
        $now = $obj->current();
        var_dump($now);
        //$this->success('返回成功', ['action' => 'test3']);
    }

    /**
     * 获取当前的天气
     */
    public function spiders(){
        $data = 'theCityName=深圳';
        $url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName';
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_HEADER,0);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,array("application/x-www-from-urlencoded;charset=utf-8","Content-length: ".strlen($data)));
        curl_setopt( $curl,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $rtn = curl_exec($curl);
        if(!curl_errno($curl)){
            return json_decode(json_encode($rtn));
        }else{
            echo 'Curl error: '.curl_errno($curl);
        }
        curl_close($curl);
    }

}
